<?php

namespace App\weapp\controller;

use App\model\CustomerModel;
use App\model\EmployeeModel;
use App\model\ProductModel;
use App\model\ProductTypeModel;
use App\model\User;
use App\model\CoinsLog;
use App\services\Pinyin;
use support\Request;

class ProductsController extends Controller
{

    public function getCates(Request $request)
    {
        $list = ProductTypeModel::select(['id', 'type_name'])->get();
        return success('分类', $list);
    }

    public function editInfo(Request $request)
    {
        $id = $request->input("id");
        $info = ProductModel::select(['id', 'name', 'specifications', 'type_id',
            'image_url', 'selling_price', 'stock',
            'is_redeemable', 'points_required'])->find($id);
        return success('获取详情', $info);
    }

    public function del(Request $request)
    {
        $id = $request->input("id");
        ProductModel::destory($id);
        return success('删除成功');
    }

    public function update(Request $request)
    {
        $data = $request->only(['id', 'name', 'specifications', 'type_id',
            'image_url', 'selling_price', 'stock',
            'is_redeemable', 'points_required']);
        $validator = validator($data, [
            'name' => 'required',
            'type_id' => 'required',
            'selling_price' => 'required',
            'stock' => 'required',
        ], [
            'name.required' => '请输入产品名称',
            'type_id.required' => '请选择类别',
            'selling_price.required' => '请填写售价',
            'stock.required' => '请填写库存'
        ]);
        if ($validator->fails()) return error($validator->errors()->first());
        $data['updated_by'] = $request->uid;
        if (!empty($data['id'])) {
            ProductModel::where('id', $data['id'])->update($data);
        } else {
            ProductModel::create($data);
        }
        return success('成功');
    }

    public function list(Request $request)
    {
        $filter = $request->post();
        $list = ProductModel::select(['id', 'name', 'specifications', 'type_id',
            'image_url', 'selling_price', 'stock',
            'is_redeemable', 'points_required', 'sale_num'])
            ->when(!empty($filter['type_id']), function ($query) use ($filter) {
                return $query->where('type_id', $filter['type_id']);
            })
            ->when(!is_null($filter['is_redeemable']), function ($query) use ($filter) {
                return $query->where('is_redeemable', $filter['is_redeemable']);
            })
            ->when(!is_null($filter['stock_status']), function ($query) use ($filter) {
                return $query->where('stock', $filter['stock_status'] === 1 ? '>' : '<=', 0);
            })
            ->when(!empty($filter['name']), function ($query) use ($filter) {
                return $query->where('name', 'like', '%' . $filter['name'] . '%');
            })
            ->when(!empty($filter['orderKey']) && !empty($filter['orderSort']), function ($query) use ($filter) {
                return $query->orderBy($filter['orderKey'], $filter['orderSort']);
            })
            ->get();
        return success('登录成功', ['list' => $list, 'total' => count($list), 'sumInfo' => [
            'stock_num' => $list->filter(function ($item) {
                return $item['stock'] > 0;
            })->sum('stock'),
            'total_num' => count($list),
            'sale_num' => $list->sum('sale_num'),
            'qian_num' => $list->filter(function ($item) {
                return $item['stock'] < 0;
            })->sum('stock')
        ]]);
    }

    public function listForCheckOut(Request $request)
    {
        $cates = ProductTypeModel::select(['id', 'type_name'])->orderBy('sort')->get();
        $cates->push(collect(['id' => null, 'type_name' => '未分类']));
        $cates = $cates->toArray();
        $products = ProductModel::select(['id', 'name', 'specifications', 'type_id',
            'image_url', 'selling_price', 'stock',
            'is_redeemable', 'points_required', 'sale_num'])->get()->toArray();
        $cateHash = [];
        foreach ($cates as $cate) {

            foreach ($products as $product) {
                $typeId = $product['type_id'];
                if ($cate['id'] === $typeId) {
                    if (isset($cateHash[$typeId])) {
                        $cateHash[$typeId]['goods_list'][] = $product;
                    } else {
                        $cateHash[$typeId] = $cate;
                        $cateHash[$typeId]['goods_list'] = [$product];
                    }
                }
            }

        }

        $cates = collect($cateHash)->values();
        return success('收银商品', $cates);
    }


}
